package com.fighYo.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fighYo.commonutils.R;
import com.fighYo.edu.entity.EduTeacher;
import com.fighYo.edu.entity.vo.TeacherQuery;
import com.fighYo.edu.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2021-08-19
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController {

    @Autowired
    EduTeacherService teacherService;

    @GetMapping("getAllTeacher")
    public R getAllTeacher() {
        return R.ok().data("items", teacherService.list(null));
    }

    @DeleteMapping("removeTeacher/{id}")
    public R removeTeacher(@PathVariable("id") String id) {
        return teacherService.removeById(id)?R.ok():R.error();
    }

//    @GetMapping("teacher/{cur}/{limit}")
//    public R pageListTeacher(@PathVariable("cur") long cur,
//                             @PathVariable("limit") long limit) {
//        Page<EduTeacher> teacherPage = new Page<>(cur, limit);
//        teacherService.page(teacherPage, null);
//        long total = teacherPage.getTotal();
//        List<EduTeacher> records = teacherPage.getRecords();
//        return R.ok().data("total", total).data("rows", records);
//    }

    @PostMapping("getTeacher/{cur}/{limit}")
    public R pageTeacherCondition(@PathVariable("cur") long cur,
                                  @PathVariable("limit") long limit,
                                  @RequestBody TeacherQuery teacherQuery) {
        Page<EduTeacher> teacherPage = new Page<>(cur, limit);

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        System.out.println(teacherQuery);
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if(level != null) {
            wrapper.eq("level", level);
        }

        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }

        if(!StringUtils.isEmpty(end)) {
            wrapper.ge("gmt_create", end);
        }

        teacherService.page(teacherPage, wrapper);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher) {
        return teacherService.save(teacher) ? R.ok() : R.error();
    }

    @GetMapping("getTeacherById/{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @PutMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher) {
        return teacherService.updateById(teacher) ? R.ok() : R.error();
    }

}

